package com.ewhale.feitengguest.ui.user.mine

import android.graphics.Color
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.LoginInfoDto
import com.ewhale.feitengguest.presenter.user.mine.MinePresenter
import com.ewhale.feitengguest.view.user.mine.BindAliAccountView
import com.orhanobut.hawk.Hawk
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.CheckUtil
import com.simga.library.utils.HawkKey
import com.simga.library.utils.StringUtil
import kotlinx.android.synthetic.main.activity_bind_ali_account_finish.*

class BindAliAccountFinishActivity : MBaseActivity<MinePresenter>(), BindAliAccountView {
    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
    }

    override fun getViewId(): Int {
        return R.layout.activity_bind_ali_account_finish
    }

    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

    private var realName = ""
    private var appCode = ""
    private var appPhone = ""
    private var mInfoDto: LoginInfoDto? = null

    override fun onGetBundle(bundle: Bundle?) {
        super.onGetBundle(bundle)
        realName = bundle!!.getString("realName")
        appPhone = bundle!!.getString("appPhone")
        appCode = bundle!!.getString("appCode")
    }

    override fun init(savedInstanceState: Bundle?) {
        setTitle("修改支付宝账号")
        mInfoDto = Hawk.get<LoginInfoDto>(HawkKey.USER_LOGIN_INFO, null)
        tv_realName.text = "真实姓名：$realName"
    }

    override fun initListener() {
        super.initListener()
        et_phone.addTextChangedListener {
            if (it.toString().isEmpty()) {
                btn_next.apply {
                    setNormalSolid(Color.parseColor("#EEEEEE"))
                    setNormalStroke(1, Color.parseColor("#D4D4D4"))
                    setTextColor(Color.parseColor("#BBBBC2"))
                    isEnabled = false
                }
            } else {
                btn_next.apply {
                    isEnabled = true
                    setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color))
                    setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color))
                    setTextColor(Color.parseColor("#ffffff"))
                }
            }
        }
        btn_next.setOnClickListener {
            if (!CheckUtil.checkEmail(et_phone.text.toString())
                    && !StringUtil.isMobileNO(et_phone.text.toString())) {
                showToast("请输入正确的支付宝账号")
                return@setOnClickListener
            }
            presenter.bindAliAccount(et_phone.text.toString(), appPhone, appCode)
        }
    }

    override fun getCodeSuccess() {}

    override fun bindSuccess() {
        showToast("绑定成功")
        mInfoDto!!.aliAccount = et_phone.text.toString()
        Hawk.put<LoginInfoDto>(HawkKey.USER_LOGIN_INFO, mInfoDto)
        finishResult()
    }

    companion object {
        @JvmStatic
        fun open(mContext: MBaseActivity<*>, realName: String, appCode: String, appPhone: String) {
            val bundle = Bundle()
            bundle.putString("realName", realName)
            bundle.putString("appPhone", appPhone)
            bundle.putString("appCode", appCode)
            mContext.startForResult(bundle, 1001, BindAliAccountFinishActivity::class.java)
        }
    }
}
